﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Excel2 = Microsoft.Office.Interop.Excel;

namespace LGPSDataGen.Generators
{
    class GasGenerator: Generator, IGenerator
    {
        private string pathToFile;
        //Open source data is EIA from US Gov
        private string content = "http://www.eia.gov/oog/ftparea/wogirs/xls/pswrgvwrec.xls"; 
        private DateTime mDate;
        string fullDate = "";
        public GasGenerator(DateTime theDate)
        {
            mDate = theDate;
            string timeStamp = mDate.Year + "_" + mDate.Month + "_" + mDate.Day;
            pathToFile = @"Gas" + ".xls";
            fullDate = mDate.Year + "_" + mDate.Month + "_" + mDate.Day;
        }

        public override void DownloadData()
        {
            if (!File.Exists(pathToFile))
            {
                WebClient wc = new WebClient();
                wc.DownloadFile(content, pathToFile);
            }
        }

        public override void ProcessData()
        {
            StreamReader stream = new StreamReader(@"Gas-" + fullDate + ".csv");
            List<string> dataArray = new List<string>();

            while (!stream.EndOfStream)
            {
                string s = stream.ReadLine();
                int month = 0;
                int day = 0;
                int year = 0;
                double value = 0;

                if (DateUtility.IsMonthString(s))
                {
                    month = DateUtility.ConvertMonthToInt(s);
                    day = Int32.Parse(s.Split(' ')[1].Split(',')[0]);
                    year = Int32.Parse(s.Split(',')[1]);
                    DateTime d = new DateTime(year, month, day);
                    if(d.DayOfYear >= mDate.DayOfYear)
                    {
                        value = Double.Parse(s.Split(',')[2]);
                        for (int i = 0; i < 24; i++)
                        {
                            dataArray.Add(i + "," + value.ToString());
                        }
                        break;
                    }
                }
            }
            stream.Close();

            File.WriteAllLines(@"Gas2-" + fullDate + ".csv", dataArray.ToArray());
        }

        public override void ExtractData()
        {
            //Use Excel DLL to open this raw data
            Excel2.Application app = new Excel2.Application();
            Excel2.Workbook book = app.Workbooks.Open(Directory.GetCurrentDirectory() + "\\" + pathToFile);
            Excel2.Worksheet sheet = findSheet("Data 1", book);

            List<string> dataArray = new List<string>();

            //Calculate the starting cell based off of the year 2011
            int startingCell = 977 - ((2011 - mDate.Year) * 52);

            int endCell = 52;
            if (mDate.Year == 2011)
                endCell = 28;

            for (int y = startingCell; y < endCell + startingCell; y++)
            {
                Excel2.Range range = (Excel2.Range)sheet.Cells[y, 1];
                string d = (string)range.Text;
                range = (Excel2.Range)sheet.Cells[y, 2]; //Sets up the gas value
                double value = (double)range.Value2; //This is the gas value
                dataArray.Add(d + "," + value.ToString());
            }
            File.WriteAllLines(@"Gas-" + fullDate + ".csv", dataArray.ToArray());
            book.Close();
            app.Quit();
        }

        //Find the specified sheet in the excel book
        private Excel2.Worksheet findSheet(string name, Excel2.Workbook book)
        {
            Excel2.Worksheet sheet = null;

            for (int i = 1; i <= book.Worksheets.Count; i++)
            {
                Excel2.Worksheet temp = (Excel2.Worksheet)book.Worksheets.get_Item(i);
                if (temp.Name.Equals(name))
                {
                    sheet = temp;
                }
            }

            return sheet;
        }
    }
}
